<?php

/**
 * 更新淘宝商品状态
 */

use Biz\model\wiki\Wiki as mWiki;

ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('memory_limit', '512M');
require_once '/data/wwwroot/g_vendor/autoload.php';


$file = '/tmp/offlineTaobaoGoods.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file);
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
$arr = [];
if ($sheetData && is_array($sheetData)) {
    $i = 0;
    foreach ($sheetData as $i => $item) {
        if ($i <= 1) {
            continue;
        }
        $info = [
            "channel_spu_id" => (string)trim($item['A']),
            "channel_spu_status" => (string)trim($item['B']),
            "channel_type" => (string)trim($item['C']),
        ];
        if (!empty($info['channel_spu_id']) && $info['channel_spu_id'] > 0 && $info['channel_spu_status'] == 'N') {
            $where = ['channel_spu_id' => $info['channel_spu_id'], 'channel' => mWiki::CHANNEL_TAOBAO];
            $data = ['channel_spu_status' => 0];
            if (!empty($info['channel_type'])) {
                $data['channel_type'] = ($info['channel_type'] == 'B') ? mWiki::CHANNEL_TYPE_TMALL : mWiki::CHANNEL_TYPE_TAOBAO;
            }
            mWiki::update($data, $where);
            var_dump(mWiki::log());
            $i++;
        }
        if (!empty($info['channel_spu_id']) && $info['channel_spu_id'] > 0 && $info['channel_spu_status'] == 'Y') {
            if (!empty($info['channel_type'])) {
                $channelType = ($info['channel_type'] == 'B') ? mWiki::CHANNEL_TYPE_TMALL : mWiki::CHANNEL_TYPE_TAOBAO;
                $data = [
                    'channel_type' => $channelType,
                    'channel_spu_status' => 1
                ];
                $where = [
                    'channel_spu_id' => $info['channel_spu_id'],
                    'channel' => mWiki::CHANNEL_TAOBAO,
                    'channel_type[!]' => $channelType
                ];
                mWiki::update($data, $where);
                var_dump(mWiki::log());
                $i++;
            }
        }
    }
    var_dump('更新了' . $i . '条数据');
}
